package top.xuziyi.crawler.util;

import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class ApiUtils {
    private static Logger logger = LogManager.getLogger(ApiUtils.class.getName());

    //雪球设置cookie
    public static CookieStore cookieStore = new BasicCookieStore();

    //等待一会抓数据,单位毫秒
    private static long sleepTime = 1000;

    private PoolingHttpClientConnectionManager cm;

    public void setCm(PoolingHttpClientConnectionManager cm) {
        this.cm = cm;
    }

    public String getHtmlUTF8(String url) {
        return this.getHtml(url, "UTF-8");
    }

    public String getHtmlUTF8Check(String url) {
        //第一次发起请求
        String html1 = this.getHtml(url, "UTF-8");

        try {
            //避免爬虫被封,等待一会抓数据
            Thread.sleep(sleepTime);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //第二次发起请求
        String html2 = this.getHtml(url, "UTF-8");

        //判断请求数据是否一致
        if (html1 != null && html1.equals(html2)) {
            return html1;
        } else {
            //如果不一致再次请求
            String html11 = this.getHtml(url, "UTF-8");

            try {
                //避免爬虫被封,等待一会抓数据
                Thread.sleep(sleepTime);
            } catch (Exception e) {
                e.printStackTrace();
            }

            //如果不一致再次请求2
            String html22 = this.getHtml(url, "UTF-8");
            if (html11 != null && html11.equals(html22)) {
                return html11;
            }
        }

        //两次请求都有问题就返回空
        return "";
    }

    public String getHtmlGbk(String url) {
        return this.getHtml(url, "GBK");
    }

    public String getHtmlGbkCheck(String url) {
        //第一次发起请求
        String html1 = this.getHtml(url, "GBK");

        try {
            //避免爬虫被封,等待一会抓数据
            Thread.sleep(sleepTime);
        } catch (Exception e) {
            e.printStackTrace();
        }

        String html2 = this.getHtml(url, "GBK");

        //判断请求数据是否一致
        if (html1 != null && html1.equals(html2)) {
            return html1;
        } else {
            //如果不一致再次请求
            String html11 = this.getHtml(url, "GBK");

            try {
                //避免爬虫被封,等待一会抓数据
                Thread.sleep(sleepTime);
            } catch (Exception e) {
                e.printStackTrace();
            }

            //如果不一致再次请求2
            String html22 = this.getHtml(url, "GBK");
            if (html11 != null && html11.equals(html22)) {
                return html11;
            }
        }

        //两次请求都有问题就返回空
        return "";
    }

    public String getHtmlGb2312(String url) {
        return this.getHtml(url, "gb2312");
    }

    public String getHtmlGb2312Check(String url) {
        //第一次发起请求
        String html1 = this.getHtml(url, "gb2312");

        try {
            //避免爬虫被封,等待一会抓数据
            Thread.sleep(sleepTime);
        } catch (Exception e) {
            e.printStackTrace();
        }

        String html2 = this.getHtml(url, "gb2312");

        //判断请求数据是否一致
        if (html1 != null && html1.equals(html2)) {
            return html1;
        } else {
            //如果不一致再次请求
            String html11 = this.getHtml(url, "gb2312");

            try {
                //避免爬虫被封,等待一会抓数据
                Thread.sleep(sleepTime);
            } catch (Exception e) {
                e.printStackTrace();
            }

            //如果不一致再次请求2
            String html22 = this.getHtml(url, "gb2312");

            if (html11 != null && html11.equals(html22)) {
                return html11;
            }
        }

        //两次请求都有问题就返回空
        return "";
    }

    private String getHtml(String url, String charset) {
        // 使用连接池管理器获取连接
        CloseableHttpClient httpClient = HttpClients.custom()
                .setConnectionManager(cm)
                .setDefaultCookieStore(cookieStore)
                .build();

        // 创建httpGet请求
        HttpGet httpGet = new HttpGet(url);

        //反爬虫,设置头信息
        httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 " +
                "(KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36");

        //爬取东方财富网添加头信息
        //httpGet.setHeader("Referer", "http://quote.eastmoney.com/center/gridlist.html");

        CloseableHttpResponse response = null;
        try {
            // 发起请求
            response = httpClient.execute(httpGet);

            // 判断请求是否成功
            if (response.getStatusLine().getStatusCode() == 200) {
                // 判断是否有响应体
                if (response.getEntity() != null) {
                    // 如果有响应体，则进行解析
                    return EntityUtils.toString(response.getEntity(), charset);
                } else {
                    logger.error("访问url地址成功,但没有数据:" + url);
                }
            } else {
                logger.error("访问url地址失败:" + url);
            }
        } catch (Exception e) {
            logger.error("HttpUtils的getHtml异常,url是" + url);
            e.printStackTrace();
        } finally {
            // 释放连接
            if (response != null) {
                try {
                    response.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        return null;
    }


}
